# -*- coding: utf-8 -*-

require File.join(Rails.root, "config", "environment.rb")

namespace :clean do
  desc "clean tmp dir when using file upload"
  task :tmp_upload_dir => :rails_env do
    Dir.glob(File.join(Rails.root, "public/uploads/tmp/*")) do |dir|
      date = Date.parse(File.basename(dir).split(/\-/).first)
      # 昨日以前に作られたものを削除
      FileUtils.rm_r(dir) if date < Date.today
    end
  end

  desc "clean session data"
  task :session => :rails_env do
    date = Date.today - 2
    ActiveRecord::Base.connection.execute("delete from sessions where created_at < #{date}")
  end
end
